#!/bin/sh

if test -z $1 ; then
  dir=$(ls -l ../modules/ |awk '/^d/ {print $NF}')

  for i in $dir
    do
      childDir=$(ls -l ../modules/$i/ |awk '/^d/ {print $NF}')
      flag=false;

      for j in $childDir
        do
          if [ 'src' = $j ]; then
            childDir_1=$(ls -l ../modules/$i/src |awk '/^d/ {print $NF}')

            for g in $childDir_1
              do
                if [ 'components' = $g ]
                then
                   echo ''
                elif [ 'router' = $g ]
                then
                   echo ''
                else
                   flag=true
                fi
              done
          fi
        done

      if $flag; then

        echo 'module-name => '$i
        echo '有src 打包 gulp和webpack的代码 '$i
        # webpack -p --config ./webpack_scripts/webpack.common-prod.js --module=$i
        # gulp --gulpfile ./webpack_scripts/gulpfile.js prod-module --module=$i
        webpack -p --config ./webpack_scripts/webpack.module-prod.js --module=$i && gulp --gulpfile ./webpack_scripts/gulpfile.js prod-module --module=$i
      else
        echo 'module-name static => '$i
        echo '没有src 只打包 gulp的代码 '$i
        gulp --gulpfile ./webpack_scripts/gulpfile.js prod-module --module=$i
      fi
    done
else
  childDir=$(ls -l ../modules/$1/ |awk '/^d/ {print $NF}')
  flag=false

  for j in $childDir
    do
      if [ 'src' = $j ]; then
        childDir_1=$(ls -l ../modules/$1/src |awk '/^d/ {print $NF}')

        for g in $childDir_1
          do
            if [ 'components' = $g ]
            then
               echo "is components"
            elif [ 'router' = $g ]
            then
               echo "is router"
            else
               echo "is success"
               flag=true
            fi
          done
      fi
    done

  echo '$flag => '$flag


  for z in $childDir
    do
      if [ 'src' = $z ]; then
        flag=true
      fi
    done

  echo '$flag => '$flag

  if $flag; then
    webpack -p --config ./webpack_scripts/webpack.module-prod.js --module=$1 && gulp --gulpfile ./webpack_scripts/gulpfile.js prod-module --module=$1
  else
    gulp --gulpfile ./webpack_scripts/gulpfile.js prod-module --module=$1
  fi

  # echo 'start push' $1
  # # echo ../modules/$1
  # cd ../modules/$1
  # git add .
  # git commit -m 'module-build-prod'
  # git pull
  # git push
  # cd ../../tools
  # echo 'end push' $1
fi
